from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver import WebElement
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from typing import List

from config.config import command_map

'''
    元素定位基础方法 统一使用Appium自带定位方式 例如Appium.ID
'''


def locate_element(driver: webdriver, locate_type: str, locate_value: str) -> WebElement:
    if locate_type in command_map:
        locate_strategy = command_map[locate_type]
        ele = WebDriverWait(driver, 10).until(lambda driver: driver.find_element(locate_strategy, locate_value))
        return ele
    else:
        raise ValueError(f'Unsupported command: {locate_type}')


# 查找组元素
def locate_elements(driver: webdriver, locate_type: str, locate_value: str) -> List[WebElement]:
    if locate_type in command_map:
        locate_strategy = command_map[locate_type]
        ele = WebDriverWait(driver, 10).until(lambda driver: driver.find_element(locate_strategy, locate_value))
        return ele
    else:
        raise ValueError(f'Unsupported command: {locate_type}')


if __name__ == "__main__":
    desired_caps = {
        "platformName": "Android",
        "deviceName": "Q7PDU19731008305",
        "appPackage": "com.sankuai.movie",
        "appActivity": "com.sankuai.movie.MovieMainActivity",
        "automationName": "UiAutomator2",
        # "noReset": True
    }

    print("Desired Capabilities: ", desired_caps)

    driver = webdriver.Remote("http://localhost:4723", options=UiAutomator2Options().load_capabilities(desired_caps))

    print(locate_elements(driver, "ID", "com.sankuai.movie:id/cyf"))
